﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using WallpaperEngineLib;

using WpfApp4.Models.Config;
using WpfApp4.Utilities;

namespace WpfApp4.Views
{
    /// <summary>
    /// BrowseWindow.xaml 的交互逻辑
    /// </summary>
    public partial class BrowseWindow : Window
    {
        public BrowseWindow()
        {
            InitializeComponent();
        }

        double l_bottom = 0;
        double l_right = 0;
        bool isKeyDown = false;
        private WindowLocationStyle _location = WindowLocationStyle.Normal;
        private bool _isInWindow = false;
        Regex regex = new Regex(@"^[a-z,0-9]$", RegexOptions.IgnoreCase);

        public double Right
        {
            get => SystemParameters.PrimaryScreenWidth - this.Left - body.Width - 40;
            set
            {
                this.Left = SystemParameters.PrimaryScreenWidth - value - body.Width - 40;
            }
        }

        public double Bottom
        {
            get => SystemParameters.PrimaryScreenHeight - this.Top - body.Height - 40;
            set
            {
                this.Top = SystemParameters.PrimaryScreenHeight - value - body.Height - 40;
            }
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                DragMove();

                if (Right <= 0)
                {
                    this.Left = SystemParameters.PrimaryScreenWidth - 20;
                    _location = WindowLocationStyle.Hide;
                }
                else if (Left <= 0)
                {
                    this.Right = SystemParameters.PrimaryScreenWidth - 20;
                    _location = WindowLocationStyle.Hide;
                }
                else if (Top <= 0)
                {
                    this.Bottom = SystemParameters.PrimaryScreenHeight - 20;
                    _location = WindowLocationStyle.Hide;
                }
                else if (Bottom <= 0)
                {
                    this.Top = SystemParameters.PrimaryScreenHeight - 20;
                    _location = WindowLocationStyle.Hide;
                }
                else
                {
                    _location = WindowLocationStyle.Normal;
                }
            }
            catch { }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (_location != WindowLocationStyle.Hide)
            {
                return;
            }

            var key = e.Key.ToString();
            if (regex.IsMatch(key) || e.Key == Key.CapsLock)
            {
                if (!isKeyDown)
                {
                    l_bottom = Bottom;
                    l_right = Right;

                    Bottom = 0;
                    Right = SystemParameters.PrimaryScreenWidth / 2 - MenuInfo.Instance.Settings.PositionRight;
                    isKeyDown = true;
                }
            }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            var key = e.Key.ToString();
            if (regex.IsMatch(key) || e.Key == Key.CapsLock)
            {
                Models.Time.Tick();
                if (isKeyDown && Models.Time.deltaTime > 0.2f)
                {
                    Bottom = l_bottom;
                    Right = l_right;

                    isKeyDown = false;
                }
            }
        }

        private void DockPanel_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                if (_location == WindowLocationStyle.Hide)
                {
                    if (this.Left == SystemParameters.PrimaryScreenWidth - 20)
                    {
                        this.Right = 0;
                    }
                    else if (this.Right == SystemParameters.PrimaryScreenWidth - 20)
                    {
                        this.Left = 0;
                    }
                    else if (this.Bottom == SystemParameters.PrimaryScreenHeight - 20)
                    {
                        this.Top = 0;
                    }
                    else if (this.Top == SystemParameters.PrimaryScreenHeight - 20)
                    {
                        this.Bottom = 0;
                    }
                    _isInWindow = true;
                    _location = WindowLocationStyle.Over;
                }
            }
            catch { }
        }

        private void DockPanel_MouseLeave(object sender, MouseEventArgs e)
        {
            try
            {
                WallpaperEngine.Instance.DoGetCursorPos(out WallpaperEngineLib.Models.Point point);
                if (point.X >= Left + 20 && point.X <= (Left + body.Width + 20) && point.Y >= Top + 20 && point.Y <= (Top + body.Height + 20))
                {
                    _isInWindow = true;
                }
                else
                {
                    _isInWindow = false;
                }
                if (_location == WindowLocationStyle.Over && !_isInWindow)
                {
                    if (this.Right == 0)
                    {
                        this.Left = SystemParameters.PrimaryScreenWidth - 20;
                    }
                    else if (this.Left == 0)
                    {
                        this.Right = SystemParameters.PrimaryScreenWidth - 20;
                    }
                    else if (this.Top == 0)
                    {
                        this.Bottom = SystemParameters.PrimaryScreenHeight - 20;
                    }
                    else if (this.Bottom <= 0)
                    {
                        this.Top = SystemParameters.PrimaryScreenHeight - 20;
                    }
                    _location = WindowLocationStyle.Hide;
                }
            }
            catch { }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void UpdateWindowSize(object sender, RoutedEventArgs e)
        {
            MenuItem menu = sender as MenuItem;
            var size = menu.Header.ToString();
            var windowSize = Utility.ConvertWindowSize(size);
            body.Width = windowSize.Width;
            body.Height = windowSize.Height;

        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            WindowManager.Instance.Remove(this);
        }
    }
}
